'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceLateCtrl
 * @description
 * # AttendanceAttendanceLateCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AttendanceAttendanceLateCtrl', function (DataService, openApi, $location,localStorageService, MapService, Util, $routeParams) {
        var vm = this;
        // 获取考勤详情
        var checkinfo = {};
        checkinfo = localStorageService.get("checkinfo");
        var ifPermission = false;
        this.tip = $routeParams.type == "early" ? "早退" : "迟到";
        this.atttip = $routeParams.type == "early" ? "签退成功" : "签到成功";
        // 获取考勤数据
        vm.param = Util.getLocalInfo("attParam");
        vm.param.remark = ""
        this.submit = function () {
            // 获取当前位置信息
            MapService.getcurPositon().then(function (data) {
                // 需要考勤位置
                var attPoint = {};
                attPoint.latitude = checkinfo.lat;
                attPoint.longitude = checkinfo.lng;
                var curPoint = data;
                vm.param.lat = curPoint.latitude;
                vm.param.lng = curPoint.longitude;
                vm.param.address = curPoint.address;
                // 如果外勤则跳转页面
                ifPermission = MapService.permissionDistance(attPoint, curPoint, checkinfo.offSet);
                if (ifPermission) {
                    DataService.getData(openApi.punchClock, vm.param).then(function (data) {
                        if (data.code == 1) {
                            navigator.notification.beep(1);
                            Util.Toast(vm.atttip, {
                                callback: Util.goBack
                            });
                            Util.clearLocalInfo("attParam")
                        } else {
                            Util.Toast(data.result, {
                                callback: Util.goBack
                            });
                        }
                    })
                } else {
                    // 外勤，去到外勤页面
                    vm.param.logType = 0;
                    // 删除备注
                    delete vm.param.remark;
                    Util.saveLocalInfo("attParam", vm.param);
                    $location.path("attendance/attendance_outside").replace();
                }
            })
        }
    });
